<?php

namespace app\index\controller;

use app\common\controller\Api;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use think\Db;
use think\db\exception\BindParamException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\response\Json;
use fast\Tongcheng;
use fast\Fahuo;
use fast\GoodsAdd;




use app\admin\model\litestore\Litestorespec as SpecModel;
use app\admin\model\litestore\Litestorespecvalue as SpecValueModel;



/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {


        $data = '[{"category_id1":"鲜猪肉类","category_id2":"鲜肉类","goods_name":"一级精品去蹄白条1","danwei":"公斤","desc":"","goods_no":"001","goods_price":"","goods_price2":"","goods_price4":"","goods_sort":"500"},{"category_id1":"鲜猪肉类","category_id2":"鲜肉类","goods_name":"一级精品带蹄白条","danwei":"公斤","desc":"","goods_no":"002","goods_price":"","goods_price2":"","goods_price4":"","goods_sort":""},{"category_id1":"鲜猪肉类","category_id2":"鲜肉类","goods_name":"中温去蹄白条","danwei":"公斤","desc":"","goods_no":"003","goods_price":"","goods_price2":"","goods_price4":"","goods_sort":""},{"category_id1":"鲜猪肉类","category_id2":"鲜肉类","goods_name":"中温带蹄白条","danwei":"公斤","desc":"","goods_no":"004","goods_price":"","goods_price2":"","goods_price4":"","goods_sort":""},{"category_id1":"鲜猪肉类","category_id2":"鲜肉类","goods_name":"热鲜带蹄白条","danwei":"公斤","desc":"","goods_no":"005","goods_price":"","goods_price2":"","goods_price4":"","goods_sort":""},{"category_id1":"鲜猪肉类","category_id2":"鲜肉类","goods_name":"鲜带皮带骨中段","danwei":"公斤","desc":"","goods_no":"006","goods_price":21.2,"goods_price2":"","goods_price4":"","goods_sort":""},{"category_id1":"鲜猪肉类","category_id2":"鲜肉类","goods_name":"一级精修中前段","danwei":"公斤","desc":"","goods_no":"007","goods_price":"","goods_price2":"","goods_price4":"","goods_sort":""}]';

        $data = json_decode($data,true);
        
        foreach ($data as $key => $value) {
            GoodsAdd::addData($value);
        }

        // $list = Db::table('fa_pifa_order')->select();

        // foreach ($list as $key => $value) {
        //     $map['order_id'] = $value['id'];
        //     $updata['createtime'] = $value['createtime'];
        //     $updata['status'] = $value['paystate'];
            
        //     Db::table('fa_pifa_orderitem')->where($map)->update($updata);
        // }

        // var_dump(1);
        return;
        // Fahuo::addFahuo(245);
        // $data['kdnStoreCode'] = '166333900743';
        // $data['storeCode'] = 'SD1';

        // // Tongcheng::createMendian(29);
        // Tongcheng::chaxunmendian(29);
        // Tongcheng::createMendian(31);
        // Tongcheng::chaxunmendian($data);
        // Tongcheng::createOrder(152,[]);
      

        // $SpecModel = new SpecModel;
        // $SpecValueModel = new SpecValueModel;

       

        // $reader = new Xlsx();

        // $filePath = ROOT_PATH . DS . 'public' . DS . 'goods.xlsx';
        // $insert = [];
  
        //     if (!$PHPExcel = $reader->load($filePath)) {
        //         $this->error(__('Unknown data format'));
        //     }
        //     $currentSheet = $PHPExcel->getSheet(0);  //读取文件中的第一个工作表
        //     $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
        //     $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
        //     $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);


        //     $fields = [];
        //     for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
        //         for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
        //             $cell = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow);
        //             $val = $cell->getValue();
        //             if ($val instanceof \PhpOffice\PhpSpreadsheet\RichText\RichText) {
        //                 $val = $val->getPlainText();
        //             }
        //             $fields[] = $val;
        //         }
        //     }


        //     // var_dump($fields);
        //     for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
        //         $values = [];
        //         for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
        //             $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
        //             if ($val instanceof \PhpOffice\PhpSpreadsheet\RichText\RichText) {
        //                 $val = $val->getPlainText();
        //             }
        //             $values[] = is_null($val) ? '' : $val;
        //         }
        //         $row = [];
        //         $temp = array_combine($fields, $values);
        //         // var_dump($temp);
        //         // foreach ($temp as $k => $v) {
        //         //     if (isset($fieldArr[$k]) && $k !== '') {
        //         //         $row[$fieldArr[$k]] = $v;
        //         //     }
        //         // }
        //         // if ($row) {
        //         //     $insert[] = $temp;
        //         // }
        //          $insert[] = $temp;
        //     }



        //     // foreach ($insert as $key => $value) {

        //     //     $data = [];
        //     //     $data['params']['shangjia_id'] = '31,29,30';
        //     //     $data['params']['goods_name'] = $value['商品名称'];
        //     //     $data['params']['desc'] = $value['商品名称'];
        //     //     $data['params']['image'] = '';
        //     //     $data['params']['images'] = '';
        //     //     $data['params']['category_id'] = '0';
        //     //     $data['params']['delivery_id'] = '24';
        //     //     $data['params']['spec_type'] = '10';
        //     //     $data['params']['is_zhekou'] = '0';
        //     //     $data['params']['en_time'] = 0;
        //     //     $data['params']['zhekou_price'] = 0.01;
        //     //     $data['params']['deduct_stock_type'] = '20';
        //     //     $data['params']['content'] = '';
        //     //     $data['params']['sales_initial'] = '0';
        //     //     $data['params']['sales_actual'] = '0';
        //     //     $data['params']['goods_status'] = '20';
        //     //     $data['params']['is_delete'] = '0';
        //     //     $data['params']['home_nav'] = '0';


        //     //     $data['spec']['goods_no'] = $value['商品编码'];
        //     //     $data['spec']['goods_price'] = '1';
        //     //     $data['spec']['goods_price2'] = '1';
        //     //     $data['spec']['goods_price4'] = '1';
        //     //     $data['spec']['line_price'] = '1';
        //     //     $data['spec']['stock_num'] = '1000';
        //     //     $data['spec']['danwei'] = $value['计量单位'];
        //     //     $data['spec']['goods_weight'] = '0.5';

        //     //     $model = new \app\admin\model\litestore\Litestoregoods;
        //     //     $result = $model->allowField(true)->save($data['params']);
              
        //     //     if ($result) {
        //     //         var_dump($key);
        //     //         $model->addGoodsSpec($data['params'],[],$data['spec']);      
        //     //     }
        //     // }
          






      


       


        // // $this->success('请求成功');
    }

    /**
     * 首页
     *
     */
   
    /**
     * 首页
     *
     */
    public function closeOrder()
    {


        $map['expiretime'] = ['<=',time()];
        $map['orderstate'] = 0;
        $updata['orderstate'] = 4;
        $num = Db::table('fa_pifa_order')->where($map)->update($updata);


        $this->success('请求成功',$num);
    }
}
